package com.mflex.WHSmartWhouseAd.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.adapter.TaskOrderBoxAdapter;
import com.mflex.WHSmartWhouseAd.model.CommitBOXLinkPlateModel;
import com.mflex.WHSmartWhouseAd.model.CommonRequest;
import com.mflex.WHSmartWhouseAd.model.SpotCheckPdaInfor;
import com.mflex.WHSmartWhouseAd.utils.HideKeyBoardUtil;
import com.mflex.WHSmartWhouseAd.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.ArrayList;
import java.util.List;

//import butterknife.BindView;
//import butterknife.ButterKnife;


public class QMSsampLingTaskActivity extends BaseActivity implements AdapterView.OnItemClickListener  {
    // 材料类型
    EditText stvMaterialtype;
  // 检验任务单号
    EditText stvInspectionNo;
    // 料号
    EditText stvPartNo;
    // 列表
    ListView lvList;


    // loading
    MiniLoadingDialog miniLoadingDialog;
    List<SpotCheckPdaInfor>SpotCheckPdaModelList=new ArrayList<>();
    CommitBOXLinkPlateModel model = new CommitBOXLinkPlateModel();
    private String userName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_qms_sampling_task);
//        ButterKnife.bind(this);
        // 弹窗提示
        miniLoadingDialog =WidgetUtils.getMiniLoadingDialog(this,"加载...");
        //        id绑定
        stvMaterialtype = findViewById(R.id.stv_Materialtype);
        stvInspectionNo = findViewById(R.id.stv_InspectionNo);
        stvPartNo = findViewById(R.id.stv_PartNo);
        lvList = findViewById(R.id.lvList);
        // 传递数据
        Intent intent = getIntent();
        userName = intent.getStringExtra("userName");
        // 视图按键监听器
        stvPartNo.setOnKeyListener(this);
        stvInspectionNo.setOnKeyListener(this);
        lvList.setOnItemClickListener(this);
        stvMaterialtype.setText("原材料");

        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(QMSsampLingTaskActivity.this,RawMaterialSpotCheckActivity.class);
                intent.putExtra("TaskNo",SpotCheckPdaModelList.get(position).getTaskNo());
                intent.putExtra("MPart",SpotCheckPdaModelList.get(position).getMPart());
                intent.putExtra("MaterialType",SpotCheckPdaModelList.get(position).getMaterialType());
                intent.putExtra("NeedQty",SpotCheckPdaModelList.get(position).getQty());
                intent.putExtra("BatchLot",SpotCheckPdaModelList.get(position).getERPLot());
                startActivity(intent);
            }
        });
    }
    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.stv_InspectionNo: //检验任务单号
                     chekInspectionNo();
                    break;
                case R.id.stv_PartNo:// 料号
                    chekPartNo();
                    break;
                default:
                    break;
            }
        }
        return false;
    }
// 料号回车事件触发接口
    private void chekPartNo() {
        if(StringUtils.isEmpty(stvPartNo.getText().toString())) {
            return;
        }
//      测试
//        SpotCheckPdaInfor people = new SpotCheckPdaInfor();
//        double adm =   Math.random();
//        people.setTaskNo("123"+adm);
//        people.setMPart("55555555");
//        people.setQty("50");
//        people.setERPLot("ERP");
//        people.setStatus("ok");
//        List dataList = new ArrayList<>();
//        dataList.add(people);
//        SpotCheckPdaModelList = dataList;
//        TaskOrderBoxAdapter TaskOrderBoxAdapter = new TaskOrderBoxAdapter(QMSsampLingTaskActivity.this,SpotCheckPdaModelList);
//        lvList.setAdapter(TaskOrderBoxAdapter);
//      结束
     miniLoadingDialog.show();
     XHttp.get("api/WHSmartWhousePDA/GetQMSMatTaskBind")
//             .params("taskno","")
             .params("mpart",stvPartNo.getText().toString())
             .syncRequest(false)
             .onMainThread(true)
             .execute(new SimpleCallBack<List<SpotCheckPdaInfor>>() {
                 @Override
                 public  void  onSuccess(List<SpotCheckPdaInfor> response) {
                     miniLoadingDialog.dismiss();
                     SpotCheckPdaModelList=response;
                     TaskOrderBoxAdapter TaskOrderBoxAdapter = new TaskOrderBoxAdapter(QMSsampLingTaskActivity.this,SpotCheckPdaModelList);
                     lvList.setAdapter(TaskOrderBoxAdapter);
                     stvPartNo.setText("");

                 }
                 @Override
                 public void onError(ApiException e) {
                     miniLoadingDialog.dismiss();
                     MediaPlayer mp = MediaPlayer.create(QMSsampLingTaskActivity.this,R.raw.error_voice);mp.start();
                     new MaterialDialog.Builder(QMSsampLingTaskActivity.this)
                             .iconRes(R.drawable.icon_warning)
                             .title(R.string.tip_warning)
                             .content(e.getMessage())
                             .positiveText(R.string.lab_submit)
                             .show();
                 }
             });
    }
//    检验任务单号回车事件触发接口
    private void chekInspectionNo() {
        if(StringUtils.isEmpty(stvInspectionNo.getText().toString())) {
            return;
        }
//      测试
//        SpotCheckPdaInfor people = new SpotCheckPdaInfor();
//        double adm =   Math.random();
//        people.setTaskNo("123"+adm);
//        people.setMPart("55555555");
//        people.setStatus("ok");
//        List dataList = new ArrayList<>();
//        dataList.add(people);
//        SpotCheckPdaModelList = dataList;
//        TaskOrderBoxAdapter TaskOrderBoxAdapter = new TaskOrderBoxAdapter(QMSsampLingTaskActivity.this,SpotCheckPdaModelList);
//        lvList.setAdapter(TaskOrderBoxAdapter);
//      结束
        miniLoadingDialog.show();
        XHttp.get("api/WHSmartWhousePDA/GetQMSMatTaskBind")
                .params("taskno",stvInspectionNo.getText().toString())
//                .params("mpart","")
                .syncRequest(false)
                .onMainThread(true)
                .execute(new SimpleCallBack<List<SpotCheckPdaInfor>>() {
                    @Override
                    public  void  onSuccess(List<SpotCheckPdaInfor> response) {
                        miniLoadingDialog.dismiss();
                        SpotCheckPdaModelList=response;
                        TaskOrderBoxAdapter TaskOrderBoxAdapter = new TaskOrderBoxAdapter(QMSsampLingTaskActivity.this,SpotCheckPdaModelList);
                        lvList.setAdapter(TaskOrderBoxAdapter);
                        stvInspectionNo.setText("");

                    }
                    @Override
                    public void onError(ApiException e) {
                        miniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(QMSsampLingTaskActivity.this,R.raw.error_voice);mp.start();
                        new MaterialDialog.Builder(QMSsampLingTaskActivity.this)
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

}